package com.googeioextended.youtubebetter.finch;

import java.io.FileNotFoundException;
import java.io.InputStream;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.googeioextended.youtubebetter.DetailViewActivity;
import com.googeioextended.youtubebetter.R;

/**
 * Slightly more sophisticated FinchVideo search application that allows a user
 * to type a search query and see network results update as they are received
 * from RESTful web services like gdata.youtube.com.  The results appear one by
 * one in the graphical list display as they are parsed from network data.
 */
public class FinchVideoActivity extends Activity {
    SimpleCursorAdapter mAdapter;

    private MesgEditText mSearchText;
    private static String TAG = "GoogleIO";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_query_activity);
        Resources r = getResources();

        final ListView searchList = (ListView) findViewById(R.id.video_list);
        Cursor videoCursor =
                managedQuery(FinchVideo.Videos.CONTENT_URI, null,
                        null, null, null);

        // have to reset this on a new search

        // Maps video entries from the database to views
        mAdapter = new SimpleCursorAdapter(this,
                R.layout.video_list_item,
                videoCursor,
                new String[] {
                        FinchVideo.Videos.TITLE,
                        FinchVideo.Videos.THUMB_URI_NAME,
                        FinchVideo.Videos.MEDIA_ID_NAME,
                        FinchVideo.Videos._ID
                },
                new int[] { R.id.video_text, R.id.video_thumb_icon });

        SimpleCursorAdapter.ViewBinder savb =
                new SimpleCursorAdapter.ViewBinder() {
            public boolean setViewValue(View view, Cursor cursor, int i) {
                switch (i) {
                    case FinchVideo.TITLE_COLUMN:
                        TextView tv = (TextView)
                                view.findViewById(R.id.video_text);
                        String videoText = cursor.getString(i);
                        tv.setText(videoText);

                        break;
                    case FinchVideo.THUMB_URI_COLUMN:
                        setThumbResource(view, cursor);
                        break;
                }
                final String id = cursor.getString(FinchVideo.ID_COLUMN);

    			view.setOnClickListener(new OnClickListener() {
//    				public boolean onClick(View v) {
//    					Log.d(TAG, "event " + event.getAction());
//    					if (event.getAction() == MotionEvent.ACTION_UP) {
//    					Log.d(TAG, "Recieved event " + event + " on view " + v);
//    					
//    				    Intent intent = new Intent("com.googeioextended.youtubebetter.DetailViewActivity");
//    				    intent.putExtra(DetailViewActivity.CURSOR_KEY, id);
//    				    startActivity(intent);
//    					return true;
//    					}
//    					return false;
//    				}
    				
    				public void onClick(View v) {
    					
    				    Intent intent = new Intent("com.googeioextended.youtubebetter.DetailViewActivity");
    				    intent.putExtra(DetailViewActivity.CURSOR_KEY, id);
    				    startActivity(intent);
    				}
    			});

                return true;
            }
        };

        mAdapter.setViewBinder(savb);

        searchList.setAdapter(mAdapter);

        mSearchText = (MesgEditText) findViewById(R.id.video_search_box);
        mSearchText.setMesgText(r.getString(R.string.finch_video_search));
        mSearchText.setOnEditorActionListener(
                new EditText.OnEditorActionListener() {
                    public boolean onEditorAction(TextView textView,
                                                  int actionId,
                                                  KeyEvent keyEvent)
                    {
                        // a null key event observed on some devices
                        if (null != keyEvent) {
                            int keyCode = keyEvent.getKeyCode();
                            if ((keyCode == KeyEvent.KEYCODE_ENTER) &&
                                    (keyEvent.getAction() ==
                                            KeyEvent.ACTION_DOWN))
                            {
                                // action only causes the provider to ensure
                                // the presence of some search results.
                                query();

                                return true;
                            }
                        }
                        return false;
                    }
                });

        final ImageButton refreshButton = (ImageButton)
                findViewById(R.id.video_update_button);
        refreshButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                query();
            }
        });
		refreshButton.setFocusable(true);
	}

	private void setThumbResource(View view, Cursor cursor) {
		Uri thumbUri = ContentUris.withAppendedId(FinchVideo.Videos.THUMB_URI,
				cursor.getLong(FinchVideo.ID_COLUMN));
		try {
			InputStream thumbStream = getContentResolver().openInputStream(
					thumbUri);
			ImageView iv = (ImageView) view.findViewById(R.id.video_thumb_icon);
			Bitmap bm = BitmapFactory.decodeStream(thumbStream);
			iv.setImageBitmap(bm);
		} catch (FileNotFoundException e) {
			Log.d(TAG, "could not open provider thumb: " + thumbUri);
		}
	}

    // sends the query to the finch video content provider
    private void query() {
        if (!mSearchText.searchEmpty()) {
            String queryString =
                    FinchVideo.Videos.QUERY_PARAM_NAME + "=" +
                            Uri.encode(mSearchText.getText().toString());
            Uri queryUri =
                    Uri.parse(FinchVideo.Videos.CONTENT_URI + "?" +
                            queryString);
            Cursor c = managedQuery(queryUri, null, null, null, null);
            mAdapter.changeCursor(c);
        }
    }
}
